package com.ihr360.insurance.controller.ledger;

import com.ihr360.authority.sharing.annonation.ControlMode;
import com.ihr360.authority.sharing.annonation.RestControl;
import com.ihr360.authority.sharing.privilege.AccessControlMethod;
import com.ihr360.commons.vo.PageData;
import com.ihr360.insurance.entity.request.MonthlyLedgerRequest;
import com.ihr360.insurance.entity.request.StaffLedgerRequest;
import com.ihr360.insurance.entity.response.LedgerResponse;
import com.ihr360.insurance.entity.vo.StaffBenefitVo;
import com.ihr360.insurance.model.dto.ledger.MonthlyLedger;
import com.ihr360.insurance.model.meta.DynamicRowCell;
import com.ihr360.insurance.model.vo.MonthlyLedgerVo;
import com.ihr360.insurance.service.MonthlyLedgerService;
import com.ihr360.insurance.service.StaffBenefitService;
import com.ihr360.maven.plugin.ModuleCodeStore;
import com.ihr360.maven.plugin.annotation.FunctionDefinition;
import com.ihr360.maven.plugin.annotation.ModuleDefinition;
import com.ihr360.rest.service.SimpleRestService;
import com.ihr360.rest.webmvc.AbstractRestSimpleWithLongIdController;
import com.ihr360.shared.advice.response.DisableResponseAdvice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 社保公积金方案
 *
 * @author david.wei
 */
@RestController
@RequestMapping("/monthlyLedgers")
@ModuleDefinition(mainModuleCode = ModuleCodeStore.Module.BENEFIT_CODE,
        mainModuleName = ModuleCodeStore.Module.BENEFIT_NAME,
        subModuleCode = ModuleCodeStore.Module.Benefit.STANDING_BOOK_CODE,
        subModuleName = ModuleCodeStore.Module.Benefit.STANDING_BOOK_NAME,
        hasExtendsAbstractRestController = true)
@RestControl(mode = ControlMode.ALLOW, controls = AccessControlMethod.METHODS.batchDelete)
public class MonthlyLedgerController extends AbstractRestSimpleWithLongIdController<MonthlyLedger> {

    @Autowired
    private StaffBenefitService staffBenefitService;

    @Autowired
    private MonthlyLedgerService monthlyLedgerService;

    @Override
    public SimpleRestService getSimpleRestService() {
        return monthlyLedgerService;
    }

    @FunctionDefinition(functionName = "获取台账中的具体数据列表", functionCode = ModuleCodeStore.Function.VIEW)
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public PageData<MonthlyLedgerVo> getLedgerList(@RequestParam String companyId,
                                                   @RequestParam String userId,
                                                   @RequestBody MonthlyLedgerRequest req) {
        return monthlyLedgerService.getLedgerList(companyId, userId, req);
    }

    /**
     * feign接口
     */
    @FunctionDefinition(functionName = "获取可添加到台账中的员工列表", functionCode = ModuleCodeStore.Function.VIEW)
    @RequestMapping(value = "/getAddableList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @DisableResponseAdvice
    public PageData<StaffBenefitVo> getCanAddToLedgerStaffs(@RequestParam String companyId,
                                                            @RequestParam String userId,
                                                            @RequestBody StaffLedgerRequest req) {
        return staffBenefitService.getCanAddToLedgerStaffs(companyId, userId, req.getStaffIds(), req);
    }

    /**
     * feign接口
     */
    @FunctionDefinition(functionName = "将选择的员工添加到台账", functionCode = ModuleCodeStore.Function.ADD)
    @RequestMapping(value = "/staff/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @DisableResponseAdvice
    public LedgerResponse addLedgerStaff(@RequestParam String companyId,
                                         @RequestParam String userId,
                                         @RequestBody List<StaffBenefitVo> staffBenefits,
                                         @RequestParam Integer ledgerYear,
                                         @RequestParam Integer ledgerMonth) {
        return monthlyLedgerService.addLedgerStaff(companyId, userId, staffBenefits, ledgerYear, ledgerMonth);
    }

    @FunctionDefinition(functionName = "保存修改的台账数据", functionCode = ModuleCodeStore.Function.UPDATE)
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public boolean saveLedgers(@RequestParam String userId, @RequestBody Map<Long, Map<String, DynamicRowCell>> ledgerCells) {
        monthlyLedgerService.saveLedgerCells(userId, ledgerCells);
        return true;
    }

    @FunctionDefinition(functionName = "重新核算台账数据", functionCode = ModuleCodeStore.Function.UPDATE)
    @RequestMapping(value = "/calculate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public LedgerResponse calculateLedgers(@RequestParam String companyId,
                                           @RequestParam String userId,
                                           @RequestParam Integer ledgerYear,
                                           @RequestParam Integer ledgerMonth) {
        return monthlyLedgerService.calculateLedgers(companyId, userId, ledgerYear, ledgerMonth);
    }

}